package com.stylefeng.guns.modular.empl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.InitEmplID;
import com.stylefeng.guns.core.util.InitID;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.empl.service.IEmplService;
import com.stylefeng.guns.modular.system.dao.EmplMapper;
import com.stylefeng.guns.modular.system.model.Car;
import com.stylefeng.guns.modular.system.model.Empl;

/**
 * 员工资料控制器
 *
 * @author fengshuonan
 * @Date 2018-04-01 23:08:07
 */
@Controller
@RequestMapping("/empl")
public class EmplController extends BaseController {

    private String PREFIX = "/empl/empl/";

    @Autowired
    private IEmplService emplService;
    @Autowired
    private EmplMapper emplMapper;

    /**
     * 跳转到员工资料首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "empl.html";
    }

    /**
     * 跳转到添加员工资料
     */
    @RequestMapping("/empl_add")
    public String emplAdd() {
        return PREFIX + "empl_add.html";
    }

    /**
     * 跳转到修改员工资料
     */
    @RequestMapping("/empl_update/{emplId}")
    public String emplUpdate(@PathVariable Integer emplId, Model model) {
        Empl empl = emplService.selectById(emplId);
        model.addAttribute("item",empl);
        LogObjectHolder.me().set(empl);
        return PREFIX + "empl_edit.html";
    }

    /**
     * 获取员工资料列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Page<Empl> page = new PageFactory<Empl>().defaultPage();
//    	Page<Consignee> page = new PageFactory<Consignee>().defaultPage();
    	if(ToolUtil.isEmpty(condition)){
    		page.setRecords(emplMapper.selectPage(page, null));
    		return super.packForBT(page);
    	}else{
    		EntityWrapper<Empl> entityWrapper = new EntityWrapper<>();
    		Wrapper<Empl> wrapper = entityWrapper.like("empl_name", condition);
    		page.setRecords(emplMapper.selectPage(page,wrapper));
    		return super.packForBT(page); 
    	}
    }

    /**
     * 新增员工资料
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Empl empl) {
    	empl.setEmplId(InitEmplID.getInstance().initDisorderID());
    	emplService.insert(empl);
        return SUCCESS_TIP;
    }

    /**
     * 删除员工资料
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer emplId) {
        emplService.deleteById(emplId);
        return SUCCESS_TIP;
    }

    /**
     * 修改员工资料
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Empl empl) {
        emplService.updateById(empl);
        return SUCCESS_TIP;
    }

    /**
     * 员工资料详情
     */
    @RequestMapping(value = "/detail/{emplId}")
    @ResponseBody
    public Object detail(@PathVariable("emplId") Integer emplId) {
        return emplService.selectById(emplId);
    }
}
